﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EmoneyWeiXinBLL;
using EmoneyWeiXinCommon;
using EmoneyWeiXinEntity;
using EmoneyWeiXin.Filter;
namespace EmoneyWeiXin.Controllers
{
    public class WXUserController : Controller
    {
        //
        // GET: /WXUser/
        [LoginAuthorizeAttribute]
        public ActionResult Index()
        {
            return View();
        }

        public string GetWXUserStat()
        {
            var weixinid = int.Parse(Request["weixinid"]);
            var gzCount = 0;
            var cancelCount = 0;
            var gzTotalCount = 0;
            WXUserBLL.getWXUserStat(weixinid, out gzCount, out cancelCount, out gzTotalCount);
            return gzCount.ToString() + "~" + cancelCount.ToString() + "~" + gzTotalCount;
        }

        //用户组列表
        public string QueryUserGroupList()
        {
            var strJson = "";
            var weixinid = int.Parse(Request["weixinid"]);
            //分组
            var group = UserGroupManagerBLL.getUserGroupManagerList(weixinid);
            if (group != null && group.Count > 0)
            {
                strJson = JsonTools.ToJson(group);
            }
            return strJson;
        }
        //标签列表
        public string QueryUserLabelList()
        {
            var strJson = "";
            var weixinid = int.Parse(Request["weixinid"]);
            var label = UserLabelManagerBLL.getUserLabelManagerList(weixinid);
            if (label != null && label.Count > 0)
            {
                strJson = JsonTools.ToJson(label);
            }
            return strJson;
        }

        //获取省份列表
        public string QueryProvinceList()
        {
            var strJson = "";
            var ds = UserGroupManagerBLL.GetProvinceList();
            if (ds != null && ds.Tables.Count > 0)
            {
                var table = ds.Tables[0];
                if (table != null && table.Rows.Count > 0)
                {
                    strJson = JsonTools.ToJson(table);
                }
            }
            return strJson;
        }

        //根据身份获取城市列表
        public string QueryCityList()
        {
            var pname = Request["pname"];
            var strJson = "";
            var ds = UserGroupManagerBLL.GetCityListByPname(pname);
            if (ds != null && ds.Tables.Count > 0)
            {
                var table = ds.Tables[0];
                if (table != null && table.Rows.Count > 0)
                {
                    strJson = JsonTools.ToJson(table);
                }
            }
            return strJson;
        }
        //查询粉丝数据列表
        public string QueryWXUserList()
        {
            var pindex = int.Parse(Request["pindex"]);
            var psize = int.Parse(Request["psize"]);
            var weixinid = int.Parse(Request["weixinid"]);
            var total = 0;
            var nickname = Request["nickname"];
            var sex = int.Parse(Request["sex"]);
            var province = Request["province"] == null ? "-1" : Request["province"];
            var city = Request["city"] == null ? "-1" : Request["city"]; ;
            var subscribe1 = Request["subscribe1"];
            var subStatu = Request["subStatu"];
            var subscribe2 = Request["subscribe2"];
            var groupid = (Request["groupid"] == null || Request["groupid"] == "") ? -1 : int.Parse(Request["groupid"]);
            var labelids = Request["labelids"] == null ? "" : Request["labelids"].Trim(',');
            var list = WXUserBLL.getWXUserList(pindex, psize, weixinid, out  total, nickname, sex, province, city, subscribe1, subscribe2, groupid, labelids, subStatu);
            return JsonTools.ToJson(new { total = total, list = list });
        }

        //更改粉丝组id
        public string UpdateWXUserGroup()
        {
            var re = "";
            var ire = 0;
            var weixinid = int.Parse(Request["weixinid"]);
            var wxids = Request["wxids"];//主键
            var openidlist = ConvertWxFormatString(Request["openids"]);
            var groupid = int.Parse(Request["groupid"]);//本地数据库组id
            //根据groupid查询wx_groupid
            var obj = UserGroupManagerBLL.getUserGroupManagerListByID(groupid);
            if (obj != null)
            {
                var wx_groupid = obj.wx_groupid;//微信平台上组id
                if (wx_groupid > 0)
                {
                    var str = UserGroupManagerBLL.BatchMoveUserGroup(weixinid, openidlist, wx_groupid);
                    if (str == "ok")
                    {
                        ire = WXUserBLL.UpdateWXUserGroup(wxids.Trim(','), groupid);
                    }
                    if (ire > 0)
                    {
                        re = "1";
                    }
                }
            }
            return re;
        }
        private string ConvertWxFormatString(string openids)
        {
            var openidlist = "";
            var arrOpen = openids.Trim(',').Split(',');
            for (var i = 0; i < arrOpen.Length; i++)
            {
                var ir = "\"" + arrOpen[i] + "\"";
                openidlist = openidlist + ir + ",";
            }
            return openidlist.Trim(',');
        }

        public ActionResult QueryCommunityUser()
        {
            return View();
        }


        #region  其中内容为盘后推荐股的相关方法
        [LoginAuthorizeAttribute]
        public ActionResult PanHouTJGManage()
        {
            return View();
        }
        MemcachedTools1 mmtols = new MemcachedTools1();
        public ActionResult PanHouTJGAdd()
        {
            string stockName = Request["stockName"];
            string stockCode = Request["stockCode"];
            PanHouTJGBLL bll = new PanHouTJGBLL();
            PanHouTJGEntity entity = new PanHouTJGEntity();
            entity.stockName = stockName;
            entity.stockCode = stockCode;
            entity.statu = 0;
            int count = bll.Insert(entity);
            if (count > 0)
            {
                string phtjgKey = StringTools.ChangeRealKey("PanHouTJG", "");
                mmtols.Add(phtjgKey, stockCode, 365, 0, 0);
                return Content("1");
            }
            else
            {
                return Content("0");
            }
        }

        public ActionResult GetListPHTJG()
        {
            PanHouTJGBLL bll = new PanHouTJGBLL();
            List<PanHouTJGEntity> lst = bll.GetList();
            return Content(JsonTools.ToJson(lst));
        }
        #endregion

    }
}
